package br.com.orion.sga.utils;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

//import org.apache.log4j.Logger;

public class MessageHandler {

	// private static Logger log = Logger.getLogger(MessageHandler.class);
	private static final String COLORS_RESOURCE = "colorsResource.properties";

	/**
	 * Get a msg from the resource file, format it with one parameter and place
	 * it to be rendered in the current view as a warning message.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 * @param parameter
	 *            to be applied in the message
	 */
	static public void placeWarningMessage(String msgFromResourceFile,
			String parameter) {
		placeMessage(msgFromResourceFile, parameter, FacesMessage.SEVERITY_WARN);
	}

	/**
	 * Get a msg from the resource file and place it to be rendered in the
	 * current view as a warning message.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 */
	static public void placeWarningMessage(String msgFromResourceFile) {
		placeMessage(msgFromResourceFile, FacesMessage.SEVERITY_WARN);
	}

	/**
	 * Get a msg from the resource file, format it with one parameter and place
	 * it to be rendered in the current view as an information message.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 * @param parameter
	 *            to be applied in the message
	 */
	static public void placeInfoMessage(String msgFromResourceFile,
			String parameter) {
		placeMessage(msgFromResourceFile, parameter, FacesMessage.SEVERITY_INFO);
	}

	static public void placeInfoMessage(String msgFromResourceFile,
			String[] params) {
		placeMessage(msgFromResourceFile, params, FacesMessage.SEVERITY_INFO);
	}

	static public void placeInfoMessageAddData(String msgFromResourceFile,
			String parameter) {

	}

	/**
	 * Get a msg from the resource file and place it to be rendered in the
	 * current view as an information message.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 */
	static public void placeInfoMessage(String msgFromResourceFile) {
		placeMessage(msgFromResourceFile, FacesMessage.SEVERITY_INFO);
	}

	/**
	 * Get a msg from the resource file, format it with one parameter and place
	 * it to be rendered in the current view as an error message.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 * @param parameter
	 *            to be applied in the message
	 */
	static public void placeErrorMessage(String msgFromResourceFile,
			String parameter) {
		placeMessage(msgFromResourceFile, parameter,
				FacesMessage.SEVERITY_ERROR);
	}

	/**
	 * Get a msg from the resource file and place it to be rendered in the
	 * current view as an error message.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 */
	static public void placeErrorMessage(String msgFromResourceFile) {
		placeMessage(msgFromResourceFile, FacesMessage.SEVERITY_ERROR);
	}

	/**
	 * Get a msg from the resource file, format it with one parameter and place
	 * it to be rendered in the current view.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 * @param parameter
	 *            to be applied in the message
	 * @param severity
	 *            of the message
	 */
	static private void placeMessage(String msgFromResourceFile,
			String parameter, Severity severity) {
		// Get the message from the resource file
		String msg = null;
		try {
			msg = getFromResourceFile(msgFromResourceFile);
		} catch (MissingResourceException e) {
			msg = "Internal Error - Message not found in the resource file: "
					+ msgFromResourceFile;
			severity = FacesMessage.SEVERITY_FATAL;
			outputMessage(msg, severity);
			return;
		}
		// Apply the parameter
		if (parameter != null) {
			Object[] params = { parameter };
			msg = MessageFormat.format(msg, params);
		}
		// Output the message
		outputMessage(msg, severity);
	}

	static private void placeMessage(String msgFromResourceFile,
			String[] params, Severity severity) {
		// Get the message from the resource file
		String msg = null;
		try {
			msg = getFromResourceFile(msgFromResourceFile);
		} catch (MissingResourceException e) {
			msg = "Internal Error - Message not found in the resource file: "
					+ msgFromResourceFile;
			severity = FacesMessage.SEVERITY_FATAL;
			outputMessage(msg, severity);
			return;
		}
		// Apply the parameter
		if (params != null) {
			msg = MessageFormat.format(msg, params);
		}
		// Output the message
		outputMessage(msg, severity);
	}

	/**
	 * Get a msg from the resource file and place it to be rendered in the
	 * current view.
	 *
	 * @param msgFromResourceFile
	 *            message from the resource file
	 * @param severity
	 *            of the message
	 */
	static private void placeMessage(String msgFromResourceFile,
			Severity severity) {
		// Get the message from the resource file
		String msg = msgFromResourceFile;
//		try {
//			msg = getFromResourceFile(msgFromResourceFile);
//		} catch (MissingResourceException e) {
//			msg = "Internal Error - Message not found in the resource file: "
//					+ msgFromResourceFile;
//			severity = FacesMessage.SEVERITY_FATAL;
//		}
		// Output the message
		outputMessage(msg, severity);
	}

	/**
	 * Output message to the view
	 * 
	 * @param msg
	 * @param severity
	 */
	static private void outputMessage(String msg, Severity severity) {
		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage(severity, msg, null));
	}

	/**
	 * Get a string from the resource from the resource-bunble "msg"
	 * 
	 * @param msgFromResourceFile
	 * @return string from the resoure file
	 * @throws MissingResourceException
	 */
	static public String getFromResourceFile(String msgFromResourceFile)
			throws MissingResourceException {
		FacesContext context = FacesContext.getCurrentInstance();
		ResourceBundle bundle = context.getApplication().getResourceBundle(
				context, "msg");
		return bundle.getString(msgFromResourceFile);
	}

	/**
	 * Get a string from the resource from the resource-bunble "colors"
	 * 
	 * @param msgFromResourceFile
	 * @return string from the resoure file
	 * @throws MissingResourceException
	 */
	static public String getFromColorsFile(String msgFromColorsFile) {
		Properties configProp = new Properties();
		InputStream in = MessageHandler.class.getClassLoader()
				.getResourceAsStream(COLORS_RESOURCE);
		try {
			configProp.load(in);
			return configProp.getProperty(msgFromColorsFile);
		} catch (IOException e) {
			// log.error("Internal Error - Message not found in the colors resource file: "
			// + msgFromColorsFile);
			// severity = FacesMessage.SEVERITY_FATAL;
		}

		return configProp.getProperty(msgFromColorsFile);
	}
}